<template>
    <div class="login">
        <el-form class="login-form" ref="loginForm" :model="loginForm" :rules="rules">
            <h3 class="title">RBAC</h3>

            <el-form-item prop="username">
                <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码">
                </el-input>
            </el-form-item>

            <el-form-item style="width:100%;">
                <el-button size="medium" type="primary" style="width:100%;" @click="userpwdLogin('loginForm')">
                    <span>登 录</span>
                </el-button>
            </el-form-item>

        </el-form>
    </div>
</template>

<script>
    import {login} from '@/api/system'
    import {setJwtToken} from "../lib/utils";

    export default {
        name: "Login",
        data() {
            return {
                loginForm: {
                    username: "",
                    password: "",
                },
                rules:{
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            userpwdLogin(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        login(this.loginForm.username, this.loginForm.password).then(res => {
                            setJwtToken(res.data);
                            this.$router.push("home");
                        }).catch(err => {
                            this.resetForm("loginForm");
                        });
                    } else {
                        this.$message({
                            message:"请正确填写表单",
                            type:"error"
                        })
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
    .login {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100%;
        background-image: url("../assets/img/login_bg.jpeg");
        background-size: cover;
    }

    .login-form {
        border-radius: 6px;
        background: #ffffff;
        width: 400px;
        padding: 25px 25px 5px 25px;

        .el-input {
            height: 38px;
            input {
                height: 38px;
            }
        }

    }
</style>